<?php

namespace App\Http\Controllers\Home;

use DB;
use Illuminate\Http\Request;
use EasyWeChat\Foundation\Application;
use App\Http\Controllers\Controller;
use App\Http\Controllers\QueryController;

use App\Http\Models\Post;
use App\Http\Models\Menu;

class IndexController extends Controller {

    protected $query = null;

    public function __construct(){
        $this->query = new QueryController();
    }

    public function index()
    {

        $posts = Post::orderBy('created_at','desc');
        $posts = $posts -> paginate(20);

        return view('home.index',['posts'=>$posts,'query'=>$this->query]);
    }

    //第一个 模块根据传入的参数自带相关模块数据查询功能
    //第二个 提供统一的模板查询函数
    public function posts(Request $request)
    {
        $cid = $request -> input('cid');
        $posts = Post::where('cid',$cid)->paginate(10);
        //分页处理
        $requestUri = preg_replace('/&page=\d+/',"",$request->getRequestUri());
        $posts->setPath($requestUri);

        return view('home.index',['posts'=>$posts,'query'=>$this->query]);
    }

    public function post($id)
    {
        $post = Post::find($id);

        return view('home.post',['post'=>$post,'query'=>$this->query]);
    }

}
